package com.flykoc.lightinggem.command;

import com.flykoc.lightinggem.Entry;
import com.flykoc.lightinggem.command.subcommand.GetCommand;
import com.flykoc.lightinggem.command.subcommand.ReloadCommand;
import com.flykoc.lightinggem.util.SubCommand;
import com.google.common.collect.Maps;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import java.util.Map;

/**
 * 指令处理类
 *
 * @author Zoyn
 * @since 2017-10-21
 */
public class CommandHandler implements CommandExecutor {

    private Map<String, SubCommand> subCommandMap = Maps.newHashMap();

    public CommandHandler() {
        registerSubCommand("get", new GetCommand());
        registerSubCommand("reload", new ReloadCommand());
    }

    /**
     * 注册子指令
     *
     * @param commandName 指令名
     * @param subCommand  指令实现类
     */
    private void registerSubCommand(String commandName, SubCommand subCommand) {
        if (subCommandMap.containsKey(commandName)) {
            Entry.getInstance().getLogger().warning("发现重复导入相同命令");
        }
        subCommandMap.put(commandName, subCommand);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("sdbs")) {
            if (args.length == 0) {
                // 指令提示
                sender.sendMessage("§b/sdbs get [宝石键名] §7获得该宝石");
                sender.sendMessage("§b/sdbs reload §7重载插件");
                return true;
            }
            SubCommand subCommand = subCommandMap.get(args[0]);
            if (subCommand == null) {
                sender.sendMessage("§c未知指令!");
                return true;
            }
            subCommand.execute(sender, args);
        }
        return false;
    }
}
